<template>
	<view class="container">
		<!-- <view class="tui-page-title" v-if="iswx">授权注册</view> -->
		<view class="tui-page-title" >注册</view>
		<view class="tui-form">
			<view class="tui-view-input">
				<tui-list-cell :hover="false" :lineLeft="false" backgroundColor="transparent">
					<view class="tui-cell-input">
						<tui-icon name="people" color="#6d7a87" :size="20"></tui-icon>
						<input :value="shareCode" placeholder="请输入分享人账号"  type="text" maxlength="6"/>
						<view class="tui-icon-close" v-show="password" @tap="clearInput(2)"><tui-icon name="close-fill" :size="16" color="#bfbfbf"></tui-icon></view>
					</view>
				</tui-list-cell>
				<tui-list-cell :hover="false" :lineLeft="false" backgroundColor="transparent">
					<view class="tui-cell-input">
						<tui-icon name="mobile" color="#6d7a87" :size="20"></tui-icon>
						<input :value="mobile" placeholder="请输入手机号" placeholder-class="tui-phcolor" type="number" maxlength="11" @input="inputMobile" />
						<view class="tui-icon-close" v-show="mobile" @tap="clearInput(1)"><tui-icon name="close-fill" :size="16" color="#bfbfbf"></tui-icon></view>
					</view>
				</tui-list-cell>
				<tui-list-cell :hover="false" :lineLeft="false" backgroundColor="transparent">
					<view class="tui-cell-input">
						<tui-icon name="shield" color="#6d7a87" :size="20"></tui-icon>
						<input placeholder="请输入验证码" placeholder-class="tui-phcolor" type="text" maxlength="6" @input="inputCode" />

<tui-button width="182rpx" height="56rpx" :size="24" type="danger" shape="circle" :plain="true" :disabled="disableds" @click="btnSend">{{ btnText }}</tui-button>
					
					</view>
				</tui-list-cell>
				<tui-list-cell :hover="false" :lineLeft="false" backgroundColor="transparent" v-if="iswx==false">
					<view class="tui-cell-input">
						<tui-icon name="pwd" color="#6d7a87" :size="20"></tui-icon>
						<input :value="password" placeholder="请输入密码" :password="true" placeholder-class="tui-phcolor" type="text" maxlength="40" @input="inputPwd" />
						<view class="tui-icon-close" v-show="password" @tap="clearInput(2)"><tui-icon name="close-fill" :size="16" color="#bfbfbf"></tui-icon></view>
					</view>
				</tui-list-cell>
			</view>
			<view class="tui-btn-box"><tui-button type="danger" :disabledGray="true" :shadow="true" shape="circle" @click="register">注册</tui-button></view>
			<!-- #ifdef H5 -->
			<view class="tui-btn-box">
				<tui-button type="primary" shape="circle" @click="down">下载APP</tui-button>
			</view>
			<!-- #endif -->
			<view class="tui-cell-text">
				注册代表同意
				<view class="tui-color-primary" hover-class="tui-opcity" :hover-stay-time="150" @tap="protocol">用户服务协议、隐私政策</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	computed: {
		disabled: function() {
			let bool = true;
			// #ifdef H5
			// let ua = window.navigator.userAgent.toLowerCase()
			// if (ua.match(/MicroMessenger/i) == 'micromessenger') {
			// 	if (this.mobile && this.sms_code) {
			// 		bool = false;
			// 	}
			// }else{
			// 	if (this.mobile && this.sms_code && this.password) {
			// 		bool = false;
			// 	}
			// }
			if (this.mobile && this.sms_code && this.password) {
				bool = false;
			}
			//#endif
			// #ifdef APP-PLUS
			if (this.mobile && this.sms_code && this.password) {
				bool = false;
			}
			//#endif
			return bool;
		}
	},
	onLoad: function(options) {
		/* 假如是小程序 */
		// #ifdef MP-WEIXIN
		   uni.navigateTo({
		     	url: '/pages/login/wxauthLogin'
		   })
		   return;
		// #endif
		if (options) {
			this.shareCode = options.shareCode;
			// #ifdef H5
			// let ua = window.navigator.userAgent.toLowerCase()
			// if (ua.match(/MicroMessenger/i) == 'micromessenger') {
			// 	this.iswx=true;
			// 	 this.tui.hasAutoLogin(options.shareCode);
			// 	 let res = this.tui.getUrlCode();
			// 	 if(res.code){
			// 		this.code =res.code
			// 	 }
			// }else{
			// 	this.tui.logout();
			// }
			this.tui.logout();
			//#endif
		}
		this.shareCode =this.tui.getUserReferee();
	},
	data() {
		return {
			mobile: '',
			password: '',
			shareCode:"",
			sms_code: '',
			disableds: false,
			iswx:false,
			code:'',
			jsondata:'',
			btnText: '获取验证码' //倒计时格式：(60秒)
		};
	},
	methods: {
		back() {
			uni.navigateBack();
		},
		inputCode(e) {
			this.sms_code = e.detail.value;
		},
		inputMobile: function(e) {
			this.mobile = e.detail.value;
		},
		inputPwd: function(e) {
			this.password = e.detail.value;
		},
		clearInput(type) {
			if (type == 1) {
				this.mobile = '';
			} else {
				this.password = '';
			}
		},
		down() {
			// #ifdef H5
			uni.navigateTo({
				url: '/pageschild/app/down'
			})
			// #endif
		},
		register() {
			this.logining = true;
		// 	if(this.iswx){
		// 		/* 先授权 */
		// 		if(this.code){
		// 			let datas={
		// 					mobile: this.mobile,
		// 					code: this.code,
		// 					sms_code: this.sms_code,
		// 					referee: this.shareCode,
		// 				};
							
		// 			let regs=this.tui.request('login','post',datas,1000,1,'','wxPubAuth');
		// 			Promise.resolve(regs).then((value)=>{
		// 			   if(value.code==200){
		// 				    this.tui.toast(value.message);
		// 					this.tui.setUserInfo(this.mobile,value.data.token,value.data.api_key);
		// 					if(this.tui.getShopId()){
		// 						uni.navigateTo({
		// 							url: '/pageschild/shop/offlinePay?shop_id'+this.tui.getShopId()
		// 						})
		// 						return;
		// 					}
		// 				   setTimeout(() => {
		// 				   	 uni.switchTab({
		// 				   	 	url: '/pages/my/my'
		// 				   	 })
		// 				   }, 1000)
		// 			   }else if(value.code==40006){
		// 			   	  this.isshareid=true;
		// 			   }else{
		// 				  this.tui.toast(value.message); 
		// 			   }
		// 			})
		// 		}else{
		// 			this.tui.getCode();
		// 		}
				
		
		// 	}
		    let datas={
		    		mobile: this.mobile,
		    		password: this.password,
		    		sms_code: this.sms_code,
		    		referee: this.shareCode,
		    	};
		    let regs=this.tui.request('login','post',datas,1000,1,'','webRegister');
		    Promise.resolve(regs).then((value)=>{
		    
		       if(value.code==200){
		    	    this.tui.toast(value.message);
		    		
		    	   setTimeout(() => {
		    	   	 this.tui.haslogin();
		    	   }, 1000)
		       }else{
		       	this.tui.toast(value.message);
		       }
		    })
		},
		btnSend(seconds){
			let datas={
					mobile: this.mobile,
					sms_type: 1,
				};
			if(this.iswx){
				if(this.code==''){
					this.tui.getCode();
					return;
				}
			}
			let regs=this.tui.request('login','post',datas,1000,1,'','sendSms');
			Promise.resolve(regs).then((value)=>{
			   if(value.code==200){
				    this.disableds=true;
				    this.tui.toast(value.message);
					this.btnText = '请稍候...';
					setTimeout(() => {
						this.doLoop(60);
					}, 500);
					
				
			   }else{
			   	this.tui.toast(value.message);
			   }
			})
		},
		
		doLoop: function(seconds) {
			seconds = seconds ? seconds : 60;
			this.btnText = seconds + 's后获取';
			let countdown = setInterval(() => {
				if (seconds > 0) {
					this.btnText = seconds + 's后获取';
					--seconds;
				} else {
					this.btnText = '获取验证码';
					this.disableds = false;
					clearInterval(countdown);
				}
			}, 1000);
		},
		protocol(){
			this.tui.href("/pages/login/privacy")
		}
	}
};
</script>

<style lang="scss" scoped>
.container {
	.tui-page-title {
		width: 100%;
		font-size: 48rpx;
		font-weight: bold;
		color: $uni-text-color;
		line-height: 42rpx;
		padding: 110rpx 40rpx 40rpx 40rpx;
		box-sizing: border-box;
	}
	.iswx{
		display: none;
	}
	.codes{color:#f00}
	.tui-form {
		padding-top: 50rpx;
		.tui-view-input {
			width: 100%;
			box-sizing: border-box;
			padding: 0 40rpx;
			.tui-cell-input {
				width: 100%;
				display: flex;
				align-items: center;
				padding-top: 48rpx;
				padding-bottom: $uni-spacing-col-base;
				input {
					flex: 1;
					padding-left: $uni-spacing-row-base;
				}
				.tui-icon-close {
					margin-left: auto;
				}
				.tui-btn-send {
					width: 156rpx;
					text-align: right;
					flex-shrink: 0;
					font-size: $uni-font-size-base;
					color: $uni-color-error;
					border-radius: 20rpx;
				
				
				}
				.tui-gray {
					color: $uni-text-color-placeholder;
				}
			}
		}
		.tui-cell-text {
			width: 100%;
			padding: 40rpx $uni-spacing-row-lg;
			box-sizing: border-box;
			font-size: $uni-font-size-sm;
			color: $uni-text-color-grey;
			display: flex;
			align-items: center;
			.tui-color-primary {
				color: $uni-color-pink;
				padding-left: $uni-spacing-row-sm;
			}
		}
		.tui-btn-box {
			width: 100%;
			padding: 0 $uni-spacing-row-lg;
			box-sizing: border-box;
			margin-top: 40rpx;
		}
	}
}
</style>
